define(function(require) {
    var app = require('app');
    var baseurl = "../component/thirdParty/angularDirectives/"; //从require配置的baseUrl中出发走到angularDirectives的路径

    function checkNull(obj) {
        if (obj == undefined || obj == null || obj == "" || obj.length <= 0)
            return true;
        return false;
    }

    //将传入的html内容转成html
    app.directive('compile', function($compile) {
        // directive factory creates a link function
        return function(scope, element, attrs) {
            scope.$watch(
                function(scope) {
                    // watch the 'compile' expression for changes
                    return scope.$eval(attrs.compile);
                },
                function(value) {
                    // when the 'compile' expression changes
                    // assign it into the current DOM
                    element.html(value);
                    // compile the new DOM and link it to the current
                    // scope.
                    // NOTE: we only compile .childNodes so that
                    // we don't get into infinite loop compiling ourselves
                    $compile(element.contents())(scope);
                }
            );
        };
    });

    //自动表格命令
    app.directive('zutable', function() {
        // Runs during compile
        return {
            // name: '',
            // priority: 1,
            // terminal: true,
            scope: { //用来隔离表格的scope，使得与父类scope不在一个作用域里
                zutable: "=zuTable", //使得父类通过这个参数来和标签通信,目的：暴露api接口对象，左边为标签作用域变量，右边为标签属性，=表示通过在属性中加入父类scope里的变量来绑定，
                Fid: "=zuFilterid", //过滤器对象与规则
                baseDataUrl: "=zuUrl", //数据接口地址的公共部分
            }, // {} = isolate, true = child, false/undefined = no change
            controller: function($scope, $element, $attrs, $transclude, $q, $location) { //调用顺序,2
                $scope.zutable = undefined;
                $scope.Fid; //过滤器对象与规则
                $scope.baseDataUrl;
                $scope.notify=false;//是否开始更新表格
                $scope.objId = $attrs["zuTable"]; //用来保证实例化对象的唯一性

                if (checkNull($scope.Fid)) {
                    console.warn("请指定过滤器id,或者设置缺省值");
                }

                if (checkNull($scope.baseDataUrl)) {
                    console.warn("请指定地址获取地址,或者设置缺省值");
                }

                //加载默认数据
                (function() {
                    //加载表格js文件（异步方式)
                    require([baseurl + "/table/_zuTable.js?id=" + $scope.objId], function(table) {
                        console.log("加载缺省--" + $scope.objId);
                        table.initVarible($scope, $q, $location);
                        table.defineTableName("缺省");
                        table.defineGetTableBodyUrl(baseurl + "table/data.json");
                        table.initFunctions(); //初始化表格
                    });
                })();

                $scope.$watch('notify', function(newValue, oldValue, scope) {
                    if (newValue==true) {
                        console.log("obj 刷新");
                        //等一会儿再刷新,因为当fid改变后，可能baseurl也会改变
                        $scope.zutable.defineGetTableBodyUrl($scope.baseDataUrl + "rows/filter/" + $scope.Fid + "/");
                        $scope.zutable.defineGetSingleDataUrl($scope.baseDataUrl + "rows/");
                        $scope.zutable.defineDeleteDatasUrl($scope.baseDataUrl + "rows/");
                        $scope.zutable.defineSaveSingleDataUrl($scope.baseDataUrl + "rows/");
                        $scope.zutable.defineDeleteSingleDataUrl($scope.baseDataUrl + "rows/");
                        $scope.zutable.tableRefresh();
                        $scope.notify=false;
                    }
                });
            },
            // require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent elements
            restrict: 'E', // E = Element(用作标签名), A = Attribute（用作属性）, C = Class（用作类）, M = Comment（用作注释）
            // template: '<div>fdsfdsfd</div>',
            templateUrl: function(a, $attrs) {
                return baseurl + "/table/_zuTable.html";
            },
            // replace: true,
            // transclude: true,//嵌套其他自定义标签
            compile: function(tElement, tAttrs) { //调用顺序,1
                //some code ....
                // console.log("conplile")
                return {
                    pre: function($scope, iElm, iAttrs, controller) { //pre-link:在complile完后调用,调用顺序,3
                        // console.log("pre")
                    },
                    post: function($scope, iElm, iAttrs, controller) { //post-link:在最后调用,调用顺序,4
                        // console.log("post")
                    }
                }
            },
        };
    });
});
